    package user;

    import book.Book;
    import book.Library;
    import book.PairOfUidAndBookId;
    import constant.Constant;
    import utils.AnalyzingBook;
    import utils.AnalyzingBorrowedBook;
    import utils.ScannerSingLeton;

    import java.io.IOException;
    import java.util.Scanner;

    public class NormalUser extends User{
        private PairOfUidAndBookId[] pairOfUidAndBookIds;
        private int borrowedCount;
        public Scanner scanner;
        public Library library;
    private final AnalyzingBorrowedBook analyzingBorrowedBook = new AnalyzingBorrowedBook();
        public NormalUser(String name,int userID) {
            super(name,"普通用户", userID);
            scanner = ScannerSingLeton.scannerget();
            library = Library.getLibrary();
        }
    private void loadBorredBook(){
        PairOfUidAndBookId[] allBorrwedBook;
        try{
            allBorrwedBook = analyzingBorrowedBook.loadObject(Constant.BORRED_BOOK_FILE_NAME);
            pairOfUidAndBookIds = new PairOfUidAndBookId[Constant.BORROW_BOOK_MAX_NUM];
            if(allBorrwedBook == null){
                borrowedCount = 0;
            }else{
                int allBorrwedBookLen = allBorrwedBook.length;
                if(allBorrwedBookLen > pairOfUidAndBookIds.length){
                    pairOfUidAndBookIds = new PairOfUidAndBookId[allBorrwedBookLen];
                }
                for (int i = 0; i < allBorrwedBookLen; i++) {
                    pairOfUidAndBookIds[i] = allBorrwedBook[i];
                }
                borrowedCount = allBorrwedBookLen;
            }
        }catch (IOException e){
            throw new RuntimeException();
        }
    }


    @Override
    public int dispaly() {
        System.out.println("普通⽤⼾ " + name + " 的操作菜单:");
        System.out.println("1. 查找图书");
        System.out.println("2. 打印所有的图书");
        System.out.println("3. 退出系统");
        System.out.println("4. 借阅图书");
        System.out.println("5. 归还图书");
        System.out.println("6. 查看当前个⼈借阅情况");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }
    //借阅图书
    public void borrowBook() {
        scanner.nextLine();
        System.out.println("请输入借阅书籍的ID：");
        int bookId = scanner.nextInt();
        scanner.nextLine();
        if(library.bookCount == 0){
            System.out.println("没有书籍可以借阅");
            return;
        }
        loadBorrowedBook();
        Book book = library.searchById(bookId);
        if(book == null){
            System.out.println("没有对应的书");
            return;
        }
        for (int i = 0; i < borrowedCount; i++) {
            PairOfUidAndBookId pairOfUidAndBookId = pairOfUidAndBookIds[i];
            if(pairOfUidAndBookId.getBookId() == book.getBookId()){
                if(pairOfUidAndBookId.getUserId() == getUserID()){
                    System.out.println("你已经借阅这本书了");
                    return;
                }else {
                    System.out.println("这本书已被别人借阅了");
                    return;
                }
            }
        }
        library.borrowedbook(bookId);
        PairOfUidAndBookId pairOfUidAndBookId = new PairOfUidAndBookId(getUserID(),book.getBookId());
        pairOfUidAndBookIds[borrowedCount] = pairOfUidAndBookId;
        borrowedCount++;
        storeBorrowedBook();
        System.out.println("借阅成功");
    }
    //归还图书
    public void returnBook() {
        loadBorrowedBook();
        if(borrowedCount == 0){
            System.out.println("没有用户借过书");
            return;
        }
        scanner.nextLine();
        System.out.println("输入你要归还书的ID：");
        int bookId = scanner.nextInt();
        scanner.nextLine();
        Book book = library.searchById(bookId);
        if(book == null){
            System.out.println("没有相关的书籍");
            return;
        }
        for (int i = 0; i < borrowedCount; i++) {
//如果2本书的ID，则认为是同⼀本书
            if (pairOfUidAndBookIds[i].getBookId() == book.getBookId()) {
//借阅⽤⼾也⼀样
                if (getUserID() == pairOfUidAndBookIds[i].getUserId()) {
                    library.returnBook(bookId);
                    System.out.println("图书 '" + book.getTitle() + "' 已成功归还。");
// ⽤最后⼀本替换归还的
                    pairOfUidAndBookIds[i] = pairOfUidAndBookIds[borrowedCount - 1];
// 清空最后⼀个
                    pairOfUidAndBookIds[borrowedCount - 1] = null;
                    borrowedCount--;
                    storeBorrowedBook();
                }else{
                    System.out.println("该书籍不是你借阅的书籍，不能归还："+book.getTitle());
                }
                return;
            }
        }

    }


    // 查看个⼈借阅情况
    public void viewBorrowHistory() {
        //读取当前借阅所有用户的借阅书籍的情况
        loadBorrowedBook();

        System.out.println("您的借阅情况如下：");

        if (borrowedCount == 0) {
            System.out.println("目前没有借阅记录.....");
        } else {
            boolean flg = false;

            for (int i = 0; i < borrowedCount; i++) {
                //这里只能查看属于自己借阅的情况
                //用户ID相同的情况下，使用书籍ID查询书籍
                if(pairOfUidAndBookIds[i].getUserId() == getUserID()) {
                    flg = true;
                    Book book = library.searchById(pairOfUidAndBookIds[i].getBookId());
                    System.out.println(book);
                }
            }
            if(!flg) {
                System.out.println("你没有借阅过书籍！");
            }
        }
    }
    private void loadBorrowedBook() {
        PairOfUidAndBookId[] allBorrowedBook;

        try {
            //1.先加载文件当中的借阅信息
            allBorrowedBook = analyzingBorrowedBook.loadObject(Constant.BORRED_BOOK_FILE_NAME);

            //2. 默认已借阅的图书数组大小为BORROW_BOOK_MAX_NUM，这里也可以定义到常量类
            pairOfUidAndBookIds = new PairOfUidAndBookId[Constant.BORROW_BOOK_MAX_NUM];



            //3.没有读取到已借阅的图书信息
            if (allBorrowedBook== null) {
                borrowedCount = 0;
            } else {
                //4. 查看实际读取到的数组长度是多少？ 3   ->5   10
                int allBorrowedBookLen= allBorrowedBook.length;


                //5. 如果读取到了10本书被借阅 但是当前borrowedBooks数组长度小于10
                if (allBorrowedBookLen > pairOfUidAndBookIds.length) {
                    //6. 按照实际情况进行分配数组内存
                    pairOfUidAndBookIds = new PairOfUidAndBookId[allBorrowedBookLen];
                }


                //7.把数据拷贝回到 已借阅图书信息的数组当中
                for (int i = 0; i < allBorrowedBookLen; i++) {
                    pairOfUidAndBookIds[i] = allBorrowedBook[i];
                }

                //8.更新当前实际借阅书籍的书籍数量
                borrowedCount = allBorrowedBookLen;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    private void storeBorrowedBook() {
        try{
            analyzingBorrowedBook.storeObject(pairOfUidAndBookIds,Constant.BORRED_BOOK_FILE_NAME);
        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }


    }
